package com.iot08.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.iot08.api.controller.base.BaseController;
import com.iot08.api.service.TimingSwitchService;
import com.iot08.api.service.supplement.KafkaService;
import com.iot08.api.vo.request.TimingSwitchUpdateReqVo;
import com.iot08.api.vo.request.base.BaseAccountReqVO;
import com.iot08.common.constant.ConstKafka;
import com.iot08.common.entity.app.SchoolTimetable;
import com.iot08.common.entity.app.TimingSwitch;
import com.iot08.common.utils.response.ResponseMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 定时开关机
 * @author weichao 1299101858@qq.com
 * @version 1.0
 * @date 4/28/22 3:18 PM
 */
@Slf4j
@RestController
@RequestMapping("/getway/timing_switch/")
public class TimingSwitchController extends BaseController {

    @Resource
    private KafkaService kafkaService;

    @Resource
    private TimingSwitchService timingSwitchService;


    @GetMapping("/{imei}")
    public ResponseMap getTimingSwitch (@Validated BaseAccountReqVO vo, @PathVariable("imei") String imei){
        TimingSwitch timingSwitch = timingSwitchService.findById(imei);
        if (timingSwitch == null) {

            timingSwitch = new TimingSwitch();
            timingSwitch.setId(imei);
            timingSwitch.setOperatorId(vo.getOpenid());
            timingSwitchService.add(timingSwitch);
        }
        JSONObject res = new JSONObject();
        res.put("startTime", timingSwitch.getStartTime());
        res.put("endTime", timingSwitch.getEndTime());
        res.put("switchStatus", timingSwitch.getSwitchStatus());
        res.put("week", timingSwitch.getWeek());
        return ResponseMap.success(res);
    }

    @PostMapping("/{imei}")
    public ResponseMap postTimingSwitch (@Validated TimingSwitchUpdateReqVo vo, @PathVariable("imei") String imei){
        TimingSwitch timingSwitch = timingSwitchService.findById(imei);
        timingSwitch.setOperatorId(vo.getOpenid());
        if (vo.getStartTime() != null) {
            timingSwitch.setStartTime(vo.getStartTime());
        }

        if (vo.getEndTime() != null) {
            timingSwitch.setEndTime(vo.getEndTime());
        }

        if (vo.getSwitchStatus() != null) {
            timingSwitch.setSwitchStatus(vo.getSwitchStatus());
        }
        if (vo.getWeek() != null) {
            timingSwitch.setWeek(vo.getWeek());
        }
        timingSwitchService.update(timingSwitch);

        JSONObject pushDevMsg = new JSONObject();
        pushDevMsg.put("startTime", timingSwitch.getStartTime());
        pushDevMsg.put("endTime", timingSwitch.getEndTime());
        pushDevMsg.put("status", timingSwitch.getSwitchStatus());
        pushDevMsg.put("week", timingSwitch.getWeek());
        pushDevMsg.put("type", ConstKafka.DEVICE_TYPE_setBootOff);
        kafkaService.emitDeviceDemand(imei, ConstKafka.DEVICE_TYPE_setBootOff, pushDevMsg, vo.getOpenid());
        return ResponseMap.success();
    }


}
